/**
 * BrandBigData.com Inc. Copyright (c) 2017 All Rights Reserved .
 */
package com.qibao.toolkit.util;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 线程池  ，线程池大小为cpu核数+1  ，线程名为pool-toolkit-%d
 *
 * @author wanggaowei
 * @version $Id:ThreadPool.java v0.1 2017/7/25 11:22 wanggaowei
 */
public class ThreadPool {

    public final static ExecutorService POOL = createFixExecutorService(Runtime.getRuntime().availableProcessors() * 5,
                    "pool-toolkit-%d");
    
    // 网络请求专用的，
    public final static ExecutorService NETPOOL = createFixExecutorService(Runtime.getRuntime().availableProcessors() * 10,
                    "net-pool-toolkit-%d");

    //评级前半段属于密集性任务，因此线程不需要太多，与cpu核心一样即可
    public final static ExecutorService RATING_PRE_POOL = createFixExecutorService(Runtime.getRuntime().availableProcessors(),
                   "rating-prev-%d");


    public static ExecutorService createFixExecutorService(int coreSize,  String nameFormat) {
        return new MdcThreadPoolExecutor(coreSize, coreSize,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                new ThreadFactoryBuilder().setNameFormat(nameFormat).build());
    }

}
